package com.yiba.vpn.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import com.yiba.vpn.config.Constants;
import org.springframework.util.StringUtils;


/**
 * @author liuyang
 * @since 2015年12月11日下午3:44:47
 */
public class DateUtil {
    public final static String YYYYMMDD = "yyyy-MM-dd";
    public final static String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
    public final static String YYYYMMDDHHMMSSSSS = "yyyy-MM-dd HH:mm:ss SSS";
    public final static String SXF_TIME = "yyyyMMddHHmmss";

    public final static String YYYYMMDDx = "yyyyMMdd";

    public final static String YYYYMMDDHH = "yyyyMMddHH";
    private final static int DAY_TIME = 24 * 60 * 60 * 1000;
    private final static int SENCOND_TIME = 1000;

       /*

    获取当前时间之前或之后几分钟 minute

    */

    public static String getTimeByMinute(int minute) {

        Calendar calendar = Calendar.getInstance();

        calendar.add(Calendar.MINUTE, minute);

        return new SimpleDateFormat(YYYYMMDDHHMMSS).format(calendar.getTime());

    }
    
    /*
	 * 
	 * 获取指定时间之前或之后几分钟 minute
	 * 
	 */

	public static Date getTimeByMinute(String time, int minute) {
		Calendar calendar = Calendar.getInstance();
		if (time != null) {
			Date date = parse(time, YYYYMMDDHHMMSS);
			calendar.setTime(date);
		}
		calendar.add(Calendar.MINUTE, minute);
		return calendar.getTime();

	}

    /**
     * 根据传入的Date和转换格式对日期进行格式化，并返回字符串表式形式
     *
     * @param date
     * @param format
     * @return
     */
    public static String parsedate(Date date, String format) {
        if (null == date) {
            return Constants.EMPTY_STRING;
        }

        if (StringUtils.isEmpty(format)) {
            format = YYYYMMDD;
        }

        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date).toString();
        } catch (Exception ex) {
            return Constants.EMPTY_STRING;
        }
    }

    /**
     * 计算时间有没有过期 true 没过期
     */
    public static boolean getIntervalDay(String expiredTime, Date now) {
    	boolean flag = false;
		if (StringUtils.isEmpty(expiredTime)) {
			return flag;
		}
		Date date = null;
		if(expiredTime.length() < 12) {
			date = parse(expiredTime, DateUtil.YYYYMMDD);
		}else {
			date = parse(expiredTime, DateUtil.YYYYMMDDHHMMSS);
		}
		// Date parse = parse(parse(now, DateUtil.YYYYMMDDHHMMSS),
		// DateUtil.YYYYMMDDHHMMSS);
		long time = date.getTime() - now.getTime();
		int second = (int) (time / SENCOND_TIME);
		if (second > 0) {
			flag = true;
		}
		return flag;
    }
    


    /**
     * 计算两个日期之间相隔的天数，date - anotherDate
     *
     * @return 两个日期之间相隔的天数
     */
    public static int getIntervalDay(Date dateFront, Date dateBack) {
        if (null == dateFront || null == dateBack)
            return 0;

        long time = dateBack.getTime() - dateFront.getTime();
        return (int) (time / DAY_TIME);
    }

    /**
     * 日期加减
     *
     * @param date
     * @param day
     * @return 返回加day天后的日期
     */
    public static String addDay(Date date, int day) {
        if (null == date)
            return null;

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * 日期加减
     *
     * @param date
     * @param month
     * @return 返回加month月后的日期
     */
    public static String addMonth(Date date, int month) {
        if (null == date)
            return null;

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * 将字符串形式的日期形式序转为日期对象
     *
     * @param dateStr
     * @param format
     * @return
     */
    public static Date parse(String dateStr, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(dateStr);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 将字符串形式的日期形式序转为日期对象
     *
     * @param
     * @param format
     * @return
     */
    public static String parse(Date date, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 得到给定月份该月第一天时间戳
     *
     * @param month
     * @return
     */
    public static long getFirstDayTime(String month) {
        // TODO: 利用常量
        String[] detail = month.split("-");
        Calendar cal = Calendar.getInstance();
        // 不加下面2行，就是取当前时间前一个月的第一天及最后一天
        cal.set(Calendar.YEAR, Integer.parseInt(detail[0]));
        cal.set(Calendar.MONTH, Integer.parseInt(detail[1]));
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date first = cal.getTime();
        return parse(parse(first, null), YYYYMMDD).getTime();
    }

    /**
     * 得到给定月份该月最后一天时间戳
     *
     * @param month
     * @return
     */
    public static long getLastDayTime(String month) {
        // TODO: 利用常量
        String[] detail = month.split("-");
        Calendar cal = Calendar.getInstance();
        // 不加下面2行，就是取当前时间前一个月的第一天及最后一天
        cal.set(Calendar.YEAR, Integer.parseInt(detail[0]));
        cal.set(Calendar.MONTH, Integer.parseInt(detail[1]));
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        cal.add(Calendar.DATE, 1);
        Date lastDate = cal.getTime();
        return parse(parse(lastDate, null), YYYYMMDD).getTime() - 1;
    }

    /**
     * 获得三个月之前日期
     *
     * @return
     */
    public static Date getThreeMonthDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -3);
        return calendar.getTime();
    }
    
    /**
     * 获得当前日期n月前/后的日期
     *
     * @return
     */
    public static String getTimeAfterMonth(String currentTime,Integer stamp) {
    	Date date = parse(currentTime, YYYYMMDDHHMMSS);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, stamp);
        Date time = calendar.getTime();
        return parse(time, YYYYMMDDHHMMSS);
    }

    public static String getUTCTime(String createAt) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TimeZone timeZone = TimeZone.getTimeZone("UTC");
        format.setTimeZone(timeZone);
        Long longtime = Long.valueOf(createAt);
        String d = format.format(longtime);
        Date date = null;
        try {
            date = format.parse(d);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String time = format.format(date);
        return time;

    }

    public static String getUTCDate(String createAt) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        TimeZone timeZone = TimeZone.getTimeZone("UTC");
        format.setTimeZone(timeZone);
        Long longtime = Long.valueOf(createAt);
        String d = format.format(longtime);
        Date date = null;
        try {
            date = format.parse(d);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String time = format.format(date);
        return time;

    }

    /**
     * 获取时间戳
     *
     * @param date
     * @return
     */
    public static Long getTimestamp(Date date) {
        return date.getTime();
    }
    
    /**
     * 计算两个日期的大小 true 参数1大,false参数1小 精确到秒
     * @return 两个日期之间相隔的天数
     */
    public static boolean getCompareDay(Date dateFront, Date dateBack) {
        if (null == dateFront || null == dateBack)
            return false;

        
        return dateFront.getTime() > dateBack.getTime();
       
    }
    
	/**
	 * stamp1 - stamp2  > 0 stamp1时间靠后
	 * @param stamp1
	 * @param stamp2
	 * @return
	 */
	public static Long compareTimeStamp(String stamp1,String stamp2) {
		Long long1 = Long.valueOf(stamp1);
		Long long2 = Long.valueOf(stamp2);
		return long1 - long2;
	}
}
